package cn.com.pxto.dao;

import java.util.List;

import cn.com.pxto.commons.SearchParam;
import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.pagesupport.hibernate.HibernatePageDaoSupport;
import cn.com.pxto.model.Contact;
import cn.com.pxto.model.InvalidGuest;

public class ContactDAO extends HibernatePageDaoSupport  {
	public Contact getContact(Integer cid){
		return (Contact)getTemplate().get(Contact.class, cid);
	}
	
	public void saveContact(Contact contact){
		getTemplate().save(contact);
	}
	public void saveInvalidGuest(InvalidGuest invalidGuest){
		getTemplate().save(invalidGuest);
	}
	@SuppressWarnings("unchecked")
	public List<Contact> listContactForNormal(int userId,String sort,PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select c from Contact as c where c.userId= " );
		HQL.append(userId);
		HQL.append(" order by c.");
		HQL.append(sort);
		HQL.append(" desc ");
		return getTemplate().pagedFind(HQL.toString(), pageInfo);
	}
	@SuppressWarnings("unchecked")
	public List<Contact> listContactForManager(String sort,PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select c from Contact as c " );
		HQL.append(" order by c.");
		HQL.append(sort);
		return getTemplate().pagedFind(HQL.toString(), pageInfo);
	}
	@SuppressWarnings("unchecked")
	public List<Contact> getContact(String sort,SearchParam[] params,PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select c from Contact as c where (1=1)");
		HQL.append(SearchParam.getSimpleWhereSentence(params,"c"));
		HQL.append(" order by c.");
		HQL.append(sort);
		List<Contact> list=getTemplate().pagedFind(HQL.toString(), SearchParam.getSearchValues(params), pageInfo);
		return list;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Contact> listContactByTel(String tel){
		String HQL="select c from Contact as c where c.tel like '%"+tel+"%' order by c.logonDate desc";
//		String HQL="select c from Contact as c where c.tel like '%"+tel+"%' ";
//		String HQL="select c from Contact as c where c.tel = '"+tel+"' ";
		return getTemplate().find(HQL);
	}
}
